unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzEdit, rtcInfo, rtcConn, rtcTcpCli, rtcTcpSrv;

type
  TForm1 = class(TForm)
    RzMemo1: TRzMemo;
    Button1: TButton;
    Button2: TButton;
    RtcTcpServer1: TRtcTcpServer;
    procedure Button1Click(Sender: TObject);
    procedure RtcTcpClient1Disconnect(Sender: TRtcConnection);
    procedure RtcTcpClient1Connect(Sender: TRtcConnection);
    procedure Button2Click(Sender: TObject);
    procedure RtcTcpServer1DataReceived(Sender: TRtcConnection);
    procedure RtcTcpServer1ListenStart(Sender: TRtcConnection);
  private
    { Private declarations }
    FNumb:Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Math;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  RtcTcpServer1.ServerAddr:='127.0.0.1';
  RtcTcpServer1.ServerPort:='1862';
  if RtcTcpServer1.isListening then
  begin
    RtcTcpServer1.StopListen;
  end;  
  RtcTcpServer1.Listen();
  FNumb:=0;
  RzMemo1.Lines.Clear;
end;

procedure TForm1.RtcTcpClient1Disconnect(Sender: TRtcConnection);
begin
  RzMemo1.Lines.Add('disconnect');
end;

procedure TForm1.RtcTcpClient1Connect(Sender: TRtcConnection);
begin
  RzMemo1.Lines.Add('connect');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.RtcTcpServer1DataReceived(Sender: TRtcConnection);
var
  TempA:string;
begin
  TempA:=Sender.Read;
  RzMemo1.Lines.Add(Format('%S',[TempA]));
  Inc(FNumb);
  RzMemo1.Lines.Add(Format('%D',[FNumb]));
  if FNumb=3 then
  begin
    Sender.Write('00004201.00330212   394020100420130301201303063940200104002171101800111111111111111111201006201303012013030639402001040021711018001');
  end else
  begin
    Sender.Write('00004201.00330212   39402010042013030120130306394020010400217110180010000420000000000201006201303012013030639402001040021711018001');
  end;
end;

procedure TForm1.RtcTcpServer1ListenStart(Sender: TRtcConnection);
begin
  Caption:='listen start';
end;

end.
